package cn.qiyu5522.tk_ems.controller.admin;


import cn.qiyu5522.tk_ems.entity.Classroom;
import cn.qiyu5522.tk_ems.entity.dto.ClassroomDTO;
import cn.qiyu5522.tk_ems.entity.dto.QueryParamDTO;
import cn.qiyu5522.tk_ems.myanotation.WebLog;
import cn.qiyu5522.tk_ems.result.R;
import cn.qiyu5522.tk_ems.service.ClassroomService;
import cn.qiyu5522.tk_ems.utils.RedisKey;
import cn.qiyu5522.tk_ems.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Tonkey
 * @since 2023-02-07
 */
@Api(tags = "班级管理")
@Slf4j
@RestController
@RequestMapping("admin/classroom")
public class ClassroomController {

    @Resource
    private ClassroomService classroomService;
    @Resource
    private RedisUtil redisUtil;

    @WebLog(description = "分页条件查询班级列表")
    @ApiOperation("班级查询")
    @ApiImplicitParam(name = "queryParamDTO", value = "条件查询对象", required = true, dataTypeClass = QueryParamDTO.class)
    @PostMapping("/query")
    public R query(@Validated @RequestBody QueryParamDTO<ClassroomDTO> queryParamDTO) {
        return classroomService.queryMajorByPageCondition(queryParamDTO);
    }

    @WebLog(description = "班级新增--批量")
    @ApiOperation("班级新增")
    @ApiImplicitParam(name = "classroomDTOS", value = "班级新增", required = true, dataTypeClass = List.class)
    @PostMapping("/add")
    public R add(@Validated @RequestBody List<ClassroomDTO> classroomDTOS) {
        return classroomService.saveMore(classroomDTOS);
    }

    @WebLog(description = "班级编辑")
    @ApiOperation("班级编辑")
    @ApiImplicitParam(name = "classroom", value = "班级实体类对象", required = true, dataTypeClass = Classroom.class)
    @PutMapping("/edit")
    public R edit(@Validated @RequestBody Classroom classroom) {
        boolean flag = classroomService.updateById(classroom);
        // 更新成功，刷新redis 缓存
        if(flag){
            if (flag) {
                // 编辑成功，刷新Redis中的缓存
                // 查询全部的班级数据
                QueryWrapper<Classroom> queryWrapper = new QueryWrapper<>();
                queryWrapper.select("id", "name","department_id","major_id");
                List<Classroom> classrooms = classroomService.list(queryWrapper);
                redisUtil.set(RedisKey.CLASSROOM_ID_AND_NAME, classrooms, RedisKey.EXPIRE_ONE_DAY);
            }
        }
        return flag ? R.ok().message("编辑成功") : R.error().message("编辑失败");
    }

    @WebLog(description = "班级删除")
    @ApiOperation("班级删除")
    @ApiImplicitParam(name = "ids", value = "班级编号集合", required = true, dataTypeClass = List.class)
    @PostMapping("/remove")
    public R remove(@RequestBody List<Long> ids) {
        return classroomService.removeMore(ids);
    }

    @ApiOperation("查询全部班级编号与名称")
    @GetMapping("/queryForIdAndName")
    public R queryForIdAndName() {
        List<Classroom> forIdAndName = classroomService.getForIdAndName();
        return R.ok().data("classroomListForIdAndName", forIdAndName);
    }
    // 获取班级专业树结构
    @WebLog(description = "获取班级专业树结构")
    @ApiOperation("获取班级专业树结构")
    @GetMapping("/queryDepAndMaJorAndClassroom")
    public R queryDepAndMaJorAndClassroom() {
        return classroomService.getDepAndMaJor();
    }


    @ApiOperation("检测班级名称是否重复")
    @ApiImplicitParam(name="name",value = "班级名称", required = true, dataType = "String")
    @GetMapping("/queryForCheckRepeatName/{name}")
    public R queryForCheckRepeatName(@NotBlank(message = "根据名称查询班级时,班级名称不能为空") @PathVariable String name){
        return classroomService.queryByName(name);
    }

}
